package main

import (
	"errors"
	"fmt"
	"strconv"
	"sync"
)

/*
*
演示多goroutine中，利用chan进行error的收集
*/
func main() {
	wg := sync.WaitGroup{}
	errCh := make(chan error, 10) //将错误存储在 chan中

	for i := 0; i < 2; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()

			err := ReturnError(i)
			if err != nil {
				errCh <- err
			}
		}(i)
	}

	wg.Wait()
	for e := range errCh {
		fmt.Println(e)
	}
}

func ReturnError(i int) (err error) {
	return errors.New("我是错误" + strconv.Itoa(i))
}
